﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PwEntryExtensions.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   The pw entry extensions.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace mPassRT.Utilities
{
    using KeePassLib;
    using KeePassLib.Security;

    /// <summary>The pw entry extensions.</summary>
    public static class PwEntryExtensions
    {
        #region Public Methods and Operators

        /// <summary>Gets a string from the entry.</summary>
        /// <param name="entry">The entry.</param>
        /// <param name="field">The field.</param>
        /// <param name="required">True if non-empty value is required.</param>
        /// <returns>The <see cref="string"/>.</returns>
        public static string GetString(this PwEntry entry, string field, bool required)
        {
            ProtectedString str = entry.Strings.Get(field);
            return str == null ? GetDefaultString(required) : str.ReadString().TrimEnd('\0');
        }

        #endregion

        #region Methods

        /// <summary>Gets a default string.</summary>
        /// <param name="required">True if non-empty value is required.</param>
        /// <returns>The <see cref="string"/>.</returns>
        private static string GetDefaultString(bool required)
        {
            return required ? "<Unknown>" : string.Empty;
        }

        #endregion
    }
}